using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System;

public class PopulateJsonDB : ScriptableWizard {
	
	List<string> folders = new List<string>{"BuffDebuff", "CharacterClass", "CharacterSheet", "Feat", "InventoryItem", "Race", "Skill", "SpecialAbility", "Spell", "WeaponItem"};
    
	public string dbURI = Application.dataPath;
	
	public GameObject jsonDBObj;
    
    [MenuItem ("Custom/Load JSON int DB")]
	
    static void CreateWizard () {
		ScriptableWizard.DisplayWizard("Populate Json DB", typeof(PopulateJsonDB), "Go"); 			
    }
    void OnWizardCreate () {
		
		JSONDB jsonDB = jsonDBObj.GetComponent<JSONDB>();
		var rules = new Dictionary<string, Dictionary<string, string>>();
		
		foreach(string objType in folders)
		{
			rules.Add(objType, new Dictionary<string, string>());
			
	 		string[] filePaths = Directory.GetFiles(dbURI + "\\" + objType);
			
			foreach(string fileURI in filePaths)
			{
				using (StreamReader readFile = new StreamReader(fileURI))
				{
					string content = readFile.ReadToEnd();
					
					string[] urlparts = fileURI.Split('\\');
					
					rules[objType].Add(urlparts[urlparts.Length-1], content);
					
					Debug.Log(urlparts[urlparts.Length-1]);
				}
			}
		}
		
		jsonDB.rules = rules;
    } 
}